<?php
/**
 * @copyright	Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

JLoader::register('ExercisesHelper', JPATH_COMPONENT.'/helpers/exercises.php');

class ExercisesViewQuestion extends JViewLegacy
{
	protected $form;
	protected $item;
	protected $state;

	/**
	 * Display the view
	 */
	public function display($tpl = null)
	{
		// Initialise variables.
		$this->form	= $this->get('Form');
		$this->item	= $this->get('Item');
		$this->state	= $this->get('State');

		// Check for errors.
		if (count($errors = $this->get('Errors'))) {
			JError::raiseError(500, implode("\n", $errors));
			return false;
		}

		$this->addToolbar();
                JHtml::addIncludePath(JPATH_COMPONENT.'/helpers/html');
                JHtml::_('exercise.stylesheet');
                require_once JPATH_COMPONENT . '/models/fields/exercisequestion.php';
		parent::display($tpl);
	}

	/**
	 * Add the page title and toolbar.
	 *
	 * @since	1.6
	 */
	protected function addToolbar()
	{
		JRequest::setVar('hidemainmenu', true);

		$user		= JFactory::getUser();
		$isNew		= ($this->item->id == 0);
		$checkedOut	= !($this->item->checked_out == 0 || $this->item->checked_out == $user->get('id'));
		$canDo		= ExercisesHelper::getActions();
//                echo '<pre>';
//                print_R($this->item);
//                echo '</pre>';
//                exit();
		JToolBarHelper::title($isNew ? JText::_('COM_EXERCISES_MANAGER_QUESTION_NEW') : JText::_('COM_EXERCISES_MANAGER_QUESTION_EDIT'), 'exercises-questions.png');

		// If not checked out, can save the item.
		if (!$checkedOut && ($canDo->get('core.edit')||$canDo->get('core.create'))) {
			JToolBarHelper::apply('question.apply');
			JToolBarHelper::save('question.save');
		}
		if (!$checkedOut && $canDo->get('core.create')) {
			JToolBarHelper::save2new('question.save2new');
		}
		// If an existing item, can save to a copy.
		if (!$isNew && $canDo->get('core.create')) {
			JToolBarHelper::save2copy('question.save2copy');
		}

		if (empty($this->item->id))  {
			JToolBarHelper::cancel('question.cancel');
		} else {
			JToolBarHelper::cancel('question.cancel', 'JTOOLBAR_CLOSE');
		}

		JToolBarHelper::divider();
		JToolBarHelper::help('JHELP_COMPONENTS_EXERCISES_QUESTIONS_EDIT');
	}
}
